package com.itwn.portal.controller;

import com.itwn.portal.model.vo.AddressRequest;
import com.itwn.portal.model.vo.AddressVo;
import com.itwn.portal.service.AddressService;
import com.itwn.portal.util.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/address")
@Api(tags = "收获地址管理")
public class AddressController {

    @Autowired
    private AddressService addressService;
    @Autowired
    private JwtUtil jwtUtil;

    @ApiOperation("根据用户id查询用户地址")
    @GetMapping("/findAddress")
    public ResponseEntity<List<AddressVo>> findAddress(
            @RequestHeader("Authorization") String token){
        // 从token中获取用户id
        String userId = jwtUtil.getValue(token, "id");
        List<AddressVo> result = addressService.findAddress(Long.parseLong(userId));
        return ResponseEntity.ok(result);
    }

    @ApiOperation("新增收货地址")
    @PostMapping("/save")
    public ResponseEntity saveAddress(
            @RequestBody AddressRequest request,
            @RequestHeader("Authorization") String token){
        // 从token中获取用户id
        String userId = jwtUtil.getValue(token, "id");
        addressService.saveAddress(userId,request);
        return ResponseEntity.ok().build();
    }
}
